const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");


const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        reaction: 187
    },
    clips: ["reaction", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, attRate: 18}, // 2+2
    ]
}

let charaters = {
    units : {
        name: "提钠里",
        dmgRate: 28.8,
        defence: 611,
        hp:10520,
        base: 260,
        criticalRate: 5,
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}


// 蓄力 156.96 + 69.48*4 
// e 蓄力cd-1.5
// q 40能量  lv10:100.12*6 + 122.36*6   lv12:111.24% 135.96% 

// 天赋 蓄力射击时 精通+50    每点精通 提升0.08% 蓄力与q增伤
//  1命 重击暴击率+15
//  2命 20草伤
//  4命 全员精通+60 有发生草系元素反应+60
//  6命 蓄力cd-0.9s 多产生一个分裂箭
calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
            count++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
            data.reaction += (50 + 120)
            if(weapon.skill) { weapon.skill(data); }
            data.dmgRate += data.reaction*0.08 + 20
            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
            // 2.304  3.9168
            let AZDmg = getDmg({ data:{...data, dmgZhi:(data.zhongjiDmgZhi||0), criticalRate:data.criticalRate+15}, rate: 1.5696+0.6948*5}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let AZDmgRea = getDmg({
                    data: {
                        ...data,
                        dmgZhi: (data.zhongjiDmgZhi || 0),
                        criticalRate: data.criticalRate + 15
                    },
                    rate: 1.5696 + 0.6948 * 5, rea
                }).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let qDdmg = getDmg({ data, rate: 1.1124*6+1.3596*6}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }

            let point = AZDmg * 8 + qDdmg + (data.phyDmg||0) * 0;
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            return {point, data, second, attack,msg:{AZDmg, qDdmg, AZDmgRea}}
        }))
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[16]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                });
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0];
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2];
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
